package com.jkgl.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jkgl.common.core.domain.R;
import com.jkgl.common.core.page.PaginationVO;
import com.jkgl.common.utils.SecurityUtils;
import com.jkgl.common.utils.bean.BeanUtils;
import com.jkgl.entity.UserInfoDayEntity;
import com.jkgl.model.userinfoday.UserInfoDayPagination;
import com.jkgl.model.userinfoday.UserInfoTodayDTO;
import com.jkgl.model.userinfoday.UserInfoTodayVO;
import com.jkgl.service.UserInfoDayService;
import com.jkgl.util.DurationUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author w020304m
 * @since 2025-02-11
 */
@RestController
@RequestMapping("gather/UserDay")
@RequiredArgsConstructor
public class UserInfoDayController {
    private final UserInfoDayService userInfoDayService;
    private final Snowflake snowflake;

    @GetMapping("today")
    @ApiOperation("当日数据")
    public R today(){
        Long userId = SecurityUtils.getUserId();
//        有个问题 如果一次性获取 两天
//        如果今天没有数据 那么 List只会有一个昨日数据排第一
//        分开拿--已解决
        List<UserInfoDayEntity> userInfoDay=userInfoDayService.getUserInfoToday(userId);
        List<UserInfoTodayVO> vos = BeanUtil.copyToList(userInfoDay, UserInfoTodayVO.class);
        for( UserInfoTodayVO vo : vos){
            long subTime = DurationUtil.timeDiff(vo.getSleepTime(), vo.getWakeupTime());
            vo.setTimeSubHour((int) (subTime/60));
            vo.setTimeSubMinutes((int) (subTime%60));
        }
        return R.ok(vos);
    }

    @PostMapping
    @ApiOperation("保存/更新当日数据")
    public R create(@RequestBody UserInfoTodayDTO userInfoTodayDTO){
        Long userId = SecurityUtils.getUserId();
        UserInfoDayEntity userInfoDay = BeanUtil.toBean(userInfoTodayDTO,UserInfoDayEntity.class);
        userInfoDay.setCreateTime(LocalDate.now());
        userInfoDay.setUserId(userId);
        userInfoDayService.create(userInfoDay);
        return R.ok("保存成功");
    }

    @GetMapping("list")
    @ApiOperation("获取当前用户分页天数据")
    public R list(UserInfoDayPagination pagination){
        Long userId = SecurityUtils.getUserId();
        pagination.setUserId(userId);
        IPage<UserInfoDayEntity> result=userInfoDayService.getUserList(pagination);
        PaginationVO paginationVO = BeanUtil.copyProperties(result, PaginationVO.class);
        List<UserInfoTodayVO> vos = BeanUtil.copyToList(result.getRecords(), UserInfoTodayVO.class);
//        for (UserInfoTodayVO vo : vos){
//            long subTime = DurationUtil.timeDiff(vo.getSleepTime(), vo.getSleepTime());
//            vo.setTimeSubHour((int) (subTime/60));
//            vo.setTimeSubMinutes((int) (subTime%60));
//        }
        paginationVO.setRecords(vos);
        return R.ok(paginationVO);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取某一天")
    public R day(@PathVariable("id") String id){
        if(id.isEmpty()){
            return R.fail("获取失败-请重新获取");
        }
        UserInfoDayEntity entity = userInfoDayService.getById(id);
        if(entity == null){
            return R.fail("获取失败-出现错误");
        }
        return R.ok(entity);
    }

}
